package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/9 18:02
 */
public class 从根到叶的二进制数之和1022 {
    List<String> ans;

    public List<String> binaryTreePaths(TreeNode root) {
        ans = new ArrayList<>();
        recur(root, new StringBuilder());
        return ans;
    }

    public void recur(TreeNode node, StringBuilder builder) {
        builder.append(node.val);
        if (node.left == null && node.right == null) {
            ans.add(builder.toString());
            return;
        }
        builder.append("->");
        if (node.left != null) {
            recur(node.left, new StringBuilder(builder));
        }
        if (node.right != null) {
            recur(node.right, new StringBuilder(builder));
        }
    }

}
